<?php

/**
 * sfGuardGroup actions.
 *
 * @package    suap
 * @subpackage sfGuardGroup
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class sfGuardGroupActions extends sfActions
{
    /*-----------------------
     * executeIndex()
     *-----------------------
     *   Index paginado y estandar, etc.
     */
  public function executeIndex(sfWebRequest $request)
  {
    
        //Agarro parámetro por parámetro y lo transformo en lo 
        //  útil para la query. Esto es para evitar el code injection
        //  es decir, me fijo lo que viene, si no es algo de lo esperado audito error. 

        // Armo un string derecho para el where.
        // los filtros son
        // l1) nombre del perfil

    $l1 = $request->getParameter('l1', null);
    $filtro = '1';
    
    if($l1 != null){
        $filtro = $filtro." AND (name LIKE '%".$l1."%')";
    }
    
    $q = Doctrine_Core::getTable('sfGuardGroup')
      ->createQuery('a')
      ->Where($filtro)      
      ->orderBy('name');
      

    $this->pager = new sfDoctrinePager('sfGuardGroup', sfConfig::get('app_sfGuardGroup_max_por_pagina'));
    $this->pager->setQuery($q);
    $this->pager->setPage($request->getparameter('page',1));
    $this->pager->init();

        /*
         * Si paso por acá reviento las cookies del caso.
         * Luego dejo la cookie re retorno de usuario
         * 
         */
    
    $this->getUser()->getAttributeHolder()->remove('idLaboratorio');
    $this->getUser()->getAttributeHolder()->remove('idPersona');
    $this->getUser()->getAttributeHolder()->remove('personaPagina'); 
    $this->getUser()->getAttributeHolder()->remove('personaL1'); 
    $this->getUser()->getAttributeHolder()->remove('personaL2'); 
    $this->getUser()->getAttributeHolder()->remove('personaF1'); 
    $this->getUser()->getAttributeHolder()->remove('personaF2'); 
    
  }
    

  public function executeShow(sfWebRequest $request)
  {
    $this->sf_guard_group = Doctrine_Core::getTable('sfGuardGroup')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->sf_guard_group);
    
                        // query de permisos que posee
    $this->permisosP = Doctrine_Core::getTable('sfGuardPermission')
      ->createQuery('pr')
      ->from('sfGuardPermission pr')
      ->leftJoin('pr.Groups gr')
      ->where('gr.id = ?',$this->sf_guard_group->getId())
      ->execute();      

                // query de todos los permisos menos los ya elegidos
    $this->permisosT = Doctrine_Core::getTable('sfGuardPermission')
      ->createQuery('pr')
      ->from('sfGuardPermission pr')                    
      ->andWhere('pr.id NOT IN (
            SELECT guard.id
            FROM sfGuardPermission guard
            LEFT JOIN guard.Groups gr 
            WHERE gr.id = ?
        )',$this->sf_guard_group->getId())
      ->execute();

  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new sfGuardGroupSUAPForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new sfGuardGroupSUAPForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($sf_guard_group = Doctrine_Core::getTable('sfGuardGroup')->find(array($request->getParameter('id'))), sprintf('Object sf_guard_group does not exist (%s).', $request->getParameter('id')));
    $this->form = new sfGuardGroupSUAPForm($sf_guard_group);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($sf_guard_group = Doctrine_Core::getTable('sfGuardGroup')->find(array($request->getParameter('id'))), sprintf('Object sf_guard_group does not exist (%s).', $request->getParameter('id')));
    $this->form = new sfGuardGroupSUAPForm($sf_guard_group);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($sf_guard_group = Doctrine_Core::getTable('sfGuardGroup')->find(array($request->getParameter('id'))), sprintf('Object sf_guard_group does not exist (%s).', $request->getParameter('id')));
    $sf_guard_group->delete();

    $this->redirect('sfGuardGroup/index');
  }

  
    /*-----------------------
     * executeAddPermiso()
     *-----------------------
     *   Toma groupId y permiId como parámetros y los linkea
     * previo controlar que no estén linkeados y demas.
     * 
     */
  
  public function executeAddPermiso(sfWebRequest $request)
  {

    $this->forward404Unless($sf_guard_group = Doctrine_Core::getTable('sfGuardGroup')->find(array($request->getParameter('groupId'))), sprintf('Object sf_guard_group does not exist (%s).', $request->getParameter('groupId')));
    $this->forward404Unless($sf_guard_permission = Doctrine_Core::getTable('sfGuardPermission')->find(array($request->getParameter('permId'))), sprintf('Object sf_guard_permission does not exist (%s).', $request->getParameter('permId')));
    
    $sf_guard_group->link('Permissions', $sf_guard_permission->getId()); 
    $sf_guard_group->save();
    $this->getUser()->setFlash('notice','El perfil ahora tiene el nuevo permiso');
    $this->redirect('sfGuardGroup/show?id='.$sf_guard_group->getId());

  }
  
    /*-----------------------
     * executeRemovePermiso()
     *-----------------------
     *   Toma groupId y permId como parámetros los des-linkea
     * previo controlar que no estén linkeados y demas.
     * 
     */
  
  public function executeRemovePermiso(sfWebRequest $request)
  {

    $this->forward404Unless($sf_guard_group = Doctrine_Core::getTable('sfGuardGroup')->find(array($request->getParameter('groupId'))), sprintf('Object sf_guard_group does not exist (%s).', $request->getParameter('groupId')));
    $this->forward404Unless($sf_guard_permission = Doctrine_Core::getTable('sfGuardPermission')->find(array($request->getParameter('permId'))), sprintf('Object sf_guard_permission does not exist (%s).', $request->getParameter('permId')));
    
    $sf_guard_group->unlink('Permissions', $sf_guard_permission->getId()); 
    $sf_guard_group->save();
    $this->getUser()->setFlash('notice','El perfil ahora no tiene ese permiso');
    $this->redirect('sfGuardGroup/show?id='.$sf_guard_group->getId());
    

  }
  
  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $sf_guard_group = $form->save();
      $this->getUser()->setFlash('timedNotice','Se guardaron los cambios');
      $this->redirect('sfGuardGroup/edit?id='.$sf_guard_group->getId());
    }
  }
}
